﻿using System;
using System.IO;
using System.Windows.Forms;
using VisualPureBasicIDE.CoreGUI.SolutionManager;
using WeifenLuo.WinFormsUI.Docking;

namespace VisualPureBasicIDE.CoreGUI
{
    public partial class SolutionExplorerForm : DockContent
    {
        #region Members
        private readonly SolutionManager.SolutionManager _solutionManager;
        #endregion

        #region Constructor
        public SolutionExplorerForm()
        {
            InitializeComponent();
            // TODO: Remove, it is a temporary path
            var path = Application.ExecutablePath.ToLower().Replace(@"bin\debug\visual purebasic ide.exe", @"src\SampleSolution\Solution.pbsln");

            // SOLVED: There should be only one global instance of SolutionManager
            _solutionManager = InstanceManager.GetInstance(typeof(SolutionManager.SolutionManager)) as SolutionManager.SolutionManager;
            if (_solutionManager != null)
            {
                _solutionManager.FileChanged += SolutionManager_FileChanged;
                _solutionManager.LoadSolution(/*ref*/ treeViewSolution, path);
            }

            if (treeViewSolution.Nodes.Count <= 0) return;

            treeViewSolution.ExpandAll();
            treeViewSolution.RefreshSubnodes((SolutionTreeNode)treeViewSolution.Nodes[0]);
        }
        #endregion

        #region Events
        private void formSolutionExplorer_Resize(object sender, EventArgs e)
        {
            //Resize to max height
            treeViewSolution.Height = Height - toolStripSolution.Height;
        }

        private void toolStripButtonShowAll_Click(object sender, EventArgs e)
        {
            //Toggle hidden files visibility
            treeViewSolution.ShowHiddenFiles ^= true;
            treeViewSolution.RefreshSubnodes((SolutionTreeNode)treeViewSolution.SelectedNode);
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            //Refresh selected node and subnodes
            treeViewSolution.RefreshSubnodes((SolutionTreeNode)treeViewSolution.SelectedNode);
        }

        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            //Trigger event (same as right-click on node, 'Open' item)
            var args = new SolutionActionEventArgs {Action = SolutionTreeViewActions.Open};
            OnActionRequest(sender, args);

            //SolutionTreeNode projectNode = treeViewSolution.SelectedNode as SolutionTreeNode;
            //XmlDocument doc = this._solutionManager.GetProjectXml(projectNode);
        }

        private void contextMenuStripSolution_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var action = SolutionTreeViewActions.Unknown;
            var node = treeViewSolution.SelectedNode as SolutionTreeNode;

            #region Get SolutionTreeViewActions
            if (e.ClickedItem == buildToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Build;
            }
            else if (e.ClickedItem == rebuildToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Rebuild;
            }
            else if (e.ClickedItem == cleanToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Clean;
            }
            else if (e.ClickedItem == debugToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Debug;
            }
            else if (e.ClickedItem == newItemToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddNewItem;
            }
            else if (e.ClickedItem == existingItemToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddExistingItem;
            }
            else if (e.ClickedItem == newFolderToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddNewFolder;
            }
            else if (e.ClickedItem == newProjectToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddNewProject;
            }
            else if (e.ClickedItem == existingProjectToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddExistingProject;
            }
            else if (e.ClickedItem == virtualFolderToolStripMenuItem)
            {
                action = SolutionTreeViewActions.AddVirtualFolder;
            }
            else if (e.ClickedItem == openToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Open;
            }
            else if (e.ClickedItem == openWithToolStripMenuItem)
            {
                action = SolutionTreeViewActions.OpenWith;
            }
            else if (e.ClickedItem == viewCodeToolStripMenuItem)
            {
                action = SolutionTreeViewActions.ViewCode;
            }
            else if (e.ClickedItem == viewDesignerToolStripMenuItem)
            {
                action = SolutionTreeViewActions.ViewDesigner;
            }
            else if (e.ClickedItem == includeInProjectToolStripMenuItem)
            {
                action = SolutionTreeViewActions.IncludeInProject;
            }
            else if (e.ClickedItem == excludeFromProjectToolStripMenuItem)
            {
                action = SolutionTreeViewActions.ExcludeFromProject;
            }
            else if (e.ClickedItem == openFolderInWindowsExplorerToolStripMenuItem)
            {
                action = SolutionTreeViewActions.OpenInExplorer;
            }
            else if (e.ClickedItem == renameToolStripMenuItem)
            {
                _solutionManager.EnableFileSystemWatcher = false;
                action = SolutionTreeViewActions.Rename;
            }
            else if (e.ClickedItem == propertiesToolStripMenuItem)
            {
                action = SolutionTreeViewActions.Properties;
            }
            #endregion

            treeViewSolution.Action(action, node);

            if (e.ClickedItem == addItemToolStripMenuItem || e.ClickedItem == addProjectToolStripMenuItem)
            {
                //Do not hide, this item is without event
            }
            else
            {
                contextMenuStripSolution.Hide();
            }
        }

        private static void SolutionManager_FileChanged(object sender, FileSystemEventArgs e)
        {
            // BUG: The FSW raises this event even if no solution is loaded
            //switch (e.ChangeType)
            //{
            //    case WatcherChangeTypes.Changed:
            //        // TODO: Change dialog, make it useful, this is just a demo
            //        MessageBox.Show("This file has been modified outside of the source editor. Do you want to reload it?");
            //        break;
            //}
        }

        private void treeViewSolution_ActionRequest(object sender, SolutionActionEventArgs e)
        {
            OnActionRequest(sender, e);
        }
        #endregion

        #region Bubble events
        public event EventHandler<SolutionActionEventArgs> ActionRequest;
        public void OnActionRequest(object sender, SolutionActionEventArgs e)
        {
            if (ActionRequest != null)
            {
                if (e.Node == null)
                {
                    e.Node = treeViewSolution.SelectedNode as SolutionTreeNode;
                }
                ActionRequest(sender, e);
            }
        }
        #endregion
    }
}